package com.example.huazong.myapplication.bottomnavigation;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;


import com.example.huazong.myapplication.R;
import com.example.huazong.myapplication.adapters.TabFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import static com.example.huazong.myapplication.R.id.Music;
import static com.example.huazong.myapplication.R.id.basic;

public class BottomNavigationActivity extends AppCompatActivity {
    ViewPager myViewPager;
    List<Fragment> list;
    TabFragmentPagerAdapter adapter;
    int[] arrId = {Music,R.id.Video};



    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                    //通过点击底部按钮来进行跳转界面
                case Music:
                    myViewPager.setCurrentItem(0);
                    return true;
                case R.id.Video:
                    myViewPager.setCurrentItem(1);
                    return true;
            }
            return false;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_navigation);

        final BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        myViewPager=(ViewPager)findViewById(R.id.myViewPager);


        //定义活动页面监听,用于界面滑动时，导航栏跟着变化
        myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                navigation.setSelectedItemId(arrId[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        /*myViewPager.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {

            }
        });*/


        InitView();
    }

    private void InitView() {
        list=new ArrayList<>();
        list.add(new MusicActivity());
        list.add(new VideoActivity());
        adapter=new TabFragmentPagerAdapter(getSupportFragmentManager(),list);
        myViewPager.setAdapter(adapter);
        //设置主页面为音频界面
        myViewPager.setCurrentItem(0);
    }







}
